package com.atguigu.spzx.cart.controller;

import com.atguigu.spzx.cart.service.CartService;
import com.atguigu.spzx.cart.vo.CartItemRedisQueryRespVo;
import com.atguigu.spzx.common.result.Result;
import com.atguigu.spzx.common.result.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * CartController
 * </p>
 *
 * @author flyduck
 * @since 2023/11/5
 */
@Api(tags = "购物车接口")
@RestController
@RequestMapping("/api/order/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @ApiOperation("清空购物车")
    @GetMapping("/auth/clearCart")
    public Result clearCart(){
        cartService.clearCart();
        return Result.ok();
    }

    @ApiOperation("更新购物车商品全部选中状态")
    @GetMapping("/auth/allCheckCart/{isChecked}")
    public Result allCheckCart(@PathVariable(value = "isChecked") Integer isChecked){
        cartService.allCheckCart(isChecked);
        return Result.ok();
    }

    @ApiOperation("更新购物车商品选中状态")
    @GetMapping("/auth/checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable(value = "skuId") Long skuId,
                            @PathVariable(value = "isChecked") Integer isChecked) {
        cartService.checkCart(skuId, isChecked);
        return Result.ok();
    }

    @ApiOperation("删除购物车商品")
    @DeleteMapping("/auth/deleteCart/{skuId}")
    public Result deleteCartItemBySkuId(@PathVariable("skuId") Long skuId) {
        cartService.deleteCartItemBySkuId(skuId);
        return Result.ok();
    }

    @ApiOperation("查询购物车列表")
    @GetMapping("/auth/cartList")
    public Result cartList(){
        List<CartItemRedisQueryRespVo> respVoList = cartService.getCartItemList();
        return Result.ok(respVoList);
    }

    @ApiOperation("添加购物车")
    @GetMapping("/auth/addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable Long skuId,
                            @PathVariable Integer skuNum){
        cartService.addToCart(skuId,skuNum);
        return Result.ok();
    }
}
